<?php

namespace Common\Model;

class RedpacketModel extends XuecheModel {

    protected $pk = 'packet_id';
    protected $tableName = 'redpacket';
    protected $fields = array(
        'packet_id', 'member_id', 'create_time', 'disabled', '`use`', 'price', 'inivte_id', 'source_id', 'expiration_time'
    );

    /**
     * 清空数据缓存
     * 
     */
    public function ClearAllCache() {
        
    }

    //缓存设置
    protected $_useCacheType = 'file';
    protected $_catheTime = 0;

    public function Validate($data) {
        return true;
    }

    public function GetDataById($member_id) {
        if(empty($member_id)){
            return FALSE;
        }
        return $this->where("`member_id` = '{$member_id}'")->order('packet_id DESC')->select();
    }

    public function GetDataByWhere($where = '', $order = '', $start = 0, $rows = 30, $fields = '', $hasCount = TRUE, $bigData = FALSE) {
        return parent::GetDataByWhere($where, $order, $start, $rows, $fields, $hasCount, $bigData);
    }

    public function GetCount($where) {
        $where = " 1=1 " . $where;
        return $this->where($where)->count($this->pk);
    }

    /**
     * 生成初始化邀请红包 
     * @param type $member_id 邀请人
     * @param type $be_member_id 被邀请人
     */
    public function SaveData($member_id, $source_id) {
        if (empty($member_id) || empty($source_id)) {
            return false;
        }
        $data = array(
            'member_id' => $member_id,
            'create_time' => time(),
            'price' => 200,
            'source_id' => $source_id,
            'expiration_time' => time()+3456000,//40天的有效期
            '`use`' => 'z',
        );
        return $this->add($data);
    }
    
    /**
     * 红包生成完毕，可以领取
     * @param type $paclet_id
     * @return boolean
     */
    public function UsePacketN($paclet_id){
        if($paclet_id >0){
            $this->ues = 'n';
            $this->expiration_time = time()+2592000;//30天有效期
            if($this->where("`packet_id` = '{$paclet_id}'")->save()){
                return true;
            }
        }
        return false;
    }
    /**
     * 领取红包。
     * @param type $paclet_id
     * @return boolean
     */
    public function UsePacketY($paclet_id){
        if($paclet_id >0){
            $this->ues = 'y';
            $this->expiration_time = 0;
            if($this->where("`packet_id` = '{$paclet_id}'")->save()){
                return TRUE;
            }
        }
        return false;   
    }
    
    /**
     * 获取红包id
     * @param type $member_id
     * @param type $source_id
     * @return boolean
     */
    public function getRedpacket($source_id){
        if($source_id > 0){
            $packet_id = $this->where("`source_id` = '{$source_id}'")->field($this->pk)->find();
            if($packet_id){
                return $packet_id['packet_id'];
            }
        }
        return FALSE;
    }

        /**
     * 检查红包是否过期 
     * @param type $paclet_id
     * @return boolean
     */
    public function IsExpiration($paclet_id){
        if($paclet_id){
           return false;
        }
        $time = $this->where("`packet_id` = '{$paclet_id}'")->field('expiration_time')->find();
        if($time || $time['expiration_time'] >= time()){
            return $time['expiration_time'];
        }
        return false;
    }

}
